﻿using System;
using System.Threading.Tasks;
using System.Timers;
using Android.App;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.Views;
using Android.Widget;
using mb;
using mb.Droid;

namespace libmp
{
    abstract class DFPopBase : IPopWaiter
    {
        protected SimpleDialogFragment dialogFragment;
        protected StartActivity activity { get => StartActivity.This; }
        protected float aspect = 1.0f;

        public DFPopBase() { }

        public void Show()
        {
            CreateDilaog();
            dialogFragment.Show(activity.SupportFragmentManager, "pop");
        }

        protected void CreateDilaog()
        {
            if (dialogFragment != null) return;

            //对于dialogfragment来说，container view自身的layoutparam似乎没用，必须显式设置其子节点大小来保证对话框大小
            var radius = 45;
            var ws = mb.Droid.Util.GetWindowSize();
            int width = (int) (ws.X * 0.7f);
            int height = (int) (width * aspect);
            int headerHeight = (int) (height * 0.3f);
            var container = new LinearLayout(activity)
                {
                    Background = Util.MakeRoundedRect(Util.MainColor, radius),
                        Orientation = Orientation.Vertical,
                }
                .AddViewEx(
                    new LinearLayout(activity)
                    {
                        Background = Util.MakeRoundedRect(Util.MainBKColor, new float[] { radius, radius, 0, 0 }, 0, Color.Transparent),
                    }
                    .SetGravityEx(GravityFlags.Center)
                    .AddViewEx(Util.MakeImageView("popup/em_good.png")),
                    new ViewGroup.LayoutParams(width, headerHeight)
                )
                .AddViewEx(
                    CreateView(),
                    new ViewGroup.LayoutParams(width, height - headerHeight)
                );

            dialogFragment = new mb.Droid.SimpleDialogFragment(container, false);
            dialogFragment.Transparent = true;
        }

        protected ViewGroup CreateButton(string text)
        {
            var c = 100;
            return Util.MakeFlatTextButton(text, Util.dpToPx(80), new Color[] { Util.MainBKColor, Color.Rgb(c, c, c) });
        }

        public void Close()
        {
            dialogFragment.Dismiss();
        }

        public void Update(string msg) { }
        protected abstract View CreateView();
    }

    class DFPopBox : DFPopBase
    {
        string message;
        public DFPopBox(string msg)
        {
            message = msg;
        }
        protected override View CreateView()
        {
            var root = new LinearLayout(activity)
            {
                Orientation = Android.Widget.Orientation.Vertical,
            };

            root.SetGravity(GravityFlags.Center);
            root.AddViewEx(
                    new TextView(activity)
                    {
                        Text = message,
                            TextSize = 24,
                            TextAlignment = TextAlignment.Center,
                            Gravity = GravityFlags.CenterHorizontal,
                            // Background = new ColorDrawable(Color.Red),
                    }.SetTextColorEx(Color.White),
                    new LinearLayout.LayoutParams(-1, 0, 3)
                    {
                        Gravity = GravityFlags.Center,
                            TopMargin = 30,
                            BottomMargin = 30,
                    })
                .AddViewEx(
                    CreateButtonGroup(),
                    new LinearLayout.LayoutParams(-1, 0, 1)
                    {
                        BottomMargin = 20,
                    }
                );

            return root;
        }

        protected virtual ViewGroup CreateButtonGroup()
        {
            return new LinearLayout(activity)
                {
                    Orientation = Orientation.Horizontal,
                }
                .SetGravityEx(GravityFlags.Center)
                .AddViewEx(
                    CreateButton("好的").SetClickHandler((s, e) =>
                    {
                        Close();
                    })
                );
        }
    }

    class DFPopConfirm : DFPopBox
    {
        string message;
        string[] btnTexts;
        public Action<bool> OnSelect;
        public DFPopConfirm(string msg, string[] btnTexts) : base(msg)
        {
            this.message = msg;
            this.btnTexts = btnTexts?? new [] { "取消", "确认" };
        }
        protected override ViewGroup CreateButtonGroup()
        {
            return new LinearLayout(activity)
                {
                    Orientation = Orientation.Horizontal,
                }
                .SetGravityEx(GravityFlags.Center)
                .AddViewEx(
                    CreateButton(btnTexts[0]).SetClickHandler((s, e) =>
                    {
                        Close();
                        OnSelect(false);
                    })
                )
                .AddViewEx(new View(activity), new LinearLayout.LayoutParams(Util.dpToPx(30), 0))
                .AddViewEx(
                    CreateButton(btnTexts[1]).SetClickHandler((s, e) =>
                    {
                        Close();
                        OnSelect(true);
                    })
                );
        }
    }

    class DFPopWaiter : DFPopBase
    {
        TextView tvMsg;
        TextView tvCount;
        Timer tmCount;

        string message;
        int countDown;
        public DFPopWaiter(string msg, int cd)
        {
            message = msg;
            countDown = cd;
            aspect = 0.7f;
        }

        protected override View CreateView()
        {
            var root = new LinearLayout(activity)
            {
                Orientation = Android.Widget.Orientation.Vertical,
            };

            root.SetGravity(GravityFlags.Center);
            root.AddView(
                tvMsg = new TextView(activity)
                {
                    Text = message,
                        TextSize = 24,
                        // Background = new ColorDrawable(Color.Red),
                }.SetTextColorEx(Color.White),
                new LinearLayout.LayoutParams(-2, -2)
                {
                    Gravity = GravityFlags.Center,
                        TopMargin = 30,
                        BottomMargin = 30,
                });

            if (countDown > 0)
            {
                root.AddView(
                    tvCount = new TextView(activity)
                    {
                        Text = countDown.ToString(), TextSize = 16,
                            // Background = new ColorDrawable(Color.Blue),
                    }.SetTextColorEx(Color.White),
                    new LinearLayout.LayoutParams(-2, -2) { Gravity = GravityFlags.Center, });
            }

            int sec = 0;
            tmCount = new Timer() { Interval = 500, Enabled = true };
            tmCount.Elapsed += (s, e) =>
            {
                sec++;
                activity.RunOnUiThread(() =>
                {
                    tvMsg.Text = message + ("".PadRight((sec % 5) + 1, '.'));
                    if (tvCount != null && sec % 2 == 0) tvCount.Text = (--countDown).ToString();
                });
            };

            return root;
        }
    }

    public class AlertImp : IPopManager
    {
        public void ShowConfirm(string msg, Action<bool> onSelect, string[] btnTexts)
        {
            var mb = new DFPopConfirm(msg, btnTexts);
            mb.OnSelect = onSelect;
            mb.Show();
        }

        public void ShowMsgbox(string msg)
        {
            var mb = new DFPopBox(msg);
            mb.Show();
        }

        public void ShowToast(string msg)
        {
            Toast.MakeText(Application.Context, msg, ToastLength.Short).Show();
        }

        public IPopWaiter ShowWait(string msg, int countDownSecond = 0)
        {
            var waiter = new DFPopWaiter(msg, countDownSecond);
            waiter.Show();
            return waiter;
        }

    }
}